package com.zaaach.citypicker.view;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.MmiPoint;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Bro0cL
 * @Date: 2018/2/8 10:56
 */
public class SideIndexBar extends Component implements Component.DrawTask {
    private static final String[] DEFAULT_INDEX_ITEMS = {"定位", "热门", "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","#"};

    private List<String> mIndexItems;
    private float mItemHeight; //每个index的高度
    private int mTextSize=36;      //sp
    private int mTextColor=-6710887;
    private int mTextTouchedColor=-13421773;
    private int mCurrentIndex = -1;

    private Paint mPaint;
    private Paint mTouchedPaint;

    private int mWidth;
    private int mHeight;
    private float mTopMargin;   //居中绘制，文字绘制起点和控件顶部的间隔

    private Text mOverlayTextView;
    private OnIndexTouchedChangedListener mOnIndexChangedListener;

    private int navigationBarHeight;

    public void setNavigationBarHeight(int height){
        this.navigationBarHeight = height;
    }

    public SideIndexBar(Context context) {
        this(context, null);
    }

    public SideIndexBar(Context context,  AttrSet attrs) {
        this(context, attrs, 0);
    }

    public SideIndexBar(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
        invalidate();
    }

    private void init(Context context) {
        mIndexItems = new ArrayList<>();
        mIndexItems.addAll(Arrays.asList(DEFAULT_INDEX_ITEMS));

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(new Color(mTextColor));

        mTouchedPaint = new Paint();
        mTouchedPaint.setAntiAlias(true);
        mTouchedPaint.setTextSize(mTextSize);
        mTouchedPaint.setColor(new Color(mTextTouchedColor));

        setTouchEventListener(new TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                    simulateClick();
                    MmiPoint point = touchEvent.getPointerPosition(touchEvent.getIndex());
                    float x = point.getX();
                    float y = point.getY();
                    System.out.println("x= "+x);
                    System.out.println("y= "+y);
                    switch (touchEvent.getAction()) {
                        case TouchEvent.PRIMARY_POINT_DOWN:
                        case TouchEvent.POINT_MOVE:
                            int indexSize = mIndexItems.size();
                            int touchedIndex = (int) (y / mItemHeight);
//                            touchedIndex=touchedIndex-2;
                            if (touchedIndex < 0) {
                                touchedIndex = 0;
                            }else if (touchedIndex >= indexSize) {
                                touchedIndex = indexSize - 1;
                            }

                            if (mOnIndexChangedListener != null && touchedIndex >= 0 && touchedIndex < indexSize){
                                if (touchedIndex != mCurrentIndex) {
                                    mCurrentIndex = touchedIndex;
                                    if (mOverlayTextView != null){
                                        mOverlayTextView.setVisibility(VISIBLE);
                                        mOverlayTextView.setText(mIndexItems.get(touchedIndex));
                                    }
                                    mOnIndexChangedListener.onIndexChanged(mIndexItems.get(touchedIndex), touchedIndex);
                                    invalidate();
                                }
                            }
                            break;
                        case TouchEvent.PRIMARY_POINT_UP:
                        case TouchEvent.CANCEL:
                            mCurrentIndex = -1;
                            if (mOverlayTextView != null){
                                mOverlayTextView.setVisibility(HIDE);
                            }
                            invalidate();
                            break;

                    }
                return true;
            }
        });
    }

    @Override
    public void invalidate() {
        super.invalidate();
        addDrawTask(this::onDraw);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
//        int oldh=100;
//        int h=2326;

        mWidth = getWidth();
//        if (Math.abs(h - oldh) == navigationBarHeight){
//            //底部导航栏隐藏或显示
//            mHeight = h;
//        }else {
//            //避免软键盘弹出时挤压
//            mHeight = Math.max(getHeight(), oldh);
//        }
        mHeight=getHeight();
        mItemHeight = mHeight / mIndexItems.size();

        mTopMargin = (mHeight - mItemHeight * mIndexItems.size()) / 2;
//        mWidth = getWidth();
//        if (Math.abs(h - oldh) == navigationBarHeight){
//            //底部导航栏隐藏或显示
//            mHeight = h;
//        }else {
//            //避免软键盘弹出时挤压
//            mHeight = Math.max(getHeight(), oldh);
//        }
        mItemHeight = mHeight / mIndexItems.size();
        mTopMargin = (mHeight - mItemHeight * mIndexItems.size()) / 2;


        String index;
        for (int i = 0; i < mIndexItems.size(); i++) {
            index = mIndexItems.get(i);
            Paint.FontMetrics fm = mPaint.getFontMetrics();
            canvas.drawText( i == mCurrentIndex ? mTouchedPaint : mPaint,index,
                    (mWidth - mPaint.measureText(index)) / 2,
                    mItemHeight / 2 + (fm.bottom-fm.top) / 2 - fm.bottom + mItemHeight * i + mTopMargin
                   );
        }
    }







    public SideIndexBar setOverlayTextView(Text overlay){
        this.mOverlayTextView = overlay;
        return this;
    }

    public SideIndexBar setOnIndexChangedListener(OnIndexTouchedChangedListener listener){
        this.mOnIndexChangedListener = listener;
        return this;
    }



    public interface OnIndexTouchedChangedListener{
        void onIndexChanged(String index, int position);
    }
}
